package util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 15:47:54 2018/11/22
 * @version:2.0
 */
public class HtmlUtil {
    public static void get(String url) {
        HttpClient client = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        //httpGet.setConfig(requestConfig);
        //执行 get请求
        HttpResponse response = null;
        try {
            response = client.execute(httpGet);
        } catch (IOException e) {
            //e.printStackTrace();
            System.out.println(url + "获取网页请求时出错" + e);
        }
        try {
            //返回获取实体
            HttpEntity entity = response.getEntity();
            //获取网页内容，指定编码

            String web = EntityUtils.toString(entity, "UTF-8");
        } catch (IOException e) {
            System.out.println(url + "获取网页响应时出错");
        }
    }

    public JSONObject postJsonHtml(String url, JSONObject jsonParam) {
        HttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        // 设置请求的header
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");
        StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);
        HttpResponse response = null;
        try {
            response = client.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String json2 = null;
        try {
            json2 = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(json2);

        // 打印执行结果
        return jsonObject;
    }

    public static File getImg(String url) {
        try {
            //创建client实例
            HttpClient client = HttpClients.createDefault();
            //创建httpget实例
        /*RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(50000).setConnectionRequestTimeout(50000)
                .setSocketTimeout(50000).build();*/

            HttpGet httpGet = new HttpGet(url);
            //httpGet.setConfig(requestConfig);
            HttpResponse response = null;
            response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                //得到实体
                HttpEntity entity = response.getEntity();

                byte[] data = EntityUtils.toByteArray(entity);

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");  //转换时间格式
                String time = dateFormat.format(Calendar.getInstance().getTime());  //获取当前时间
                //图片存入磁盘
                File f = FileCreator.checkExist("验证码/" + time + ".png");
                FileOutputStream fos = new FileOutputStream(f);
                fos.write(data);
                fos.close();
                System.out.println("图片下载成功!!!!");
                return f;
            }
        } catch (Exception e) {
            System.out.println("图片下载异常!!!!" + e);
        }
        return null;

    }
}
